(function () {
    'use strict';
    angular.module("myApp", ["ui.router", "ngResource"])
        .config(function ($stateProvider, $urlRouterProvider) {//路由

            $urlRouterProvider.when("", "/item");

            $stateProvider.state("PageTab", {
                url: "/item",
                templateUrl: "app/item/item.html",
                controller: "AppController"
            });
        })
        .controller("AppController", ["$scope", "Item", function ($scope, Item) {//控制器
            Item.query(function (response) {
                $scope.items = response ? response : [];
            });

            $scope.addItem = function (description) {
                new Item({
                    description: description,
                    checked: false
                }).$save(function (item) {
                    $scope.items.push(item);
                });
                $scope.newItem = "";
            };

            $scope.updateItem = function (item) {
                item.$update();
            };

            $scope.deleteItem = function (item) {
                item.$remove(function () {
                    $scope.items.splice($scope.items.indexOf(item), 1);
                });
            };
        }])
        .factory("Item", ["$resource", function ($resource) {//service
            return $resource('/demo/items/:id', {
                id: '@id'
            }, {
                update: {
                    method: "PUT"
                },
                remove: {
                    method: "DELETE"
                }
            });
        }]);
})();